package com.ruoyi.project.system.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qq.connect.QQConnectException;
import com.qq.connect.oauth.Oauth;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.user.service.LoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import java.io.IOException;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Controller
public class LoginController extends BaseController
{


    @Autowired
    private LoginService loginService;

    /*漫画首页请求*/
    private String prefix = "manhua/look";

    /**
     * qq跳转
     * @throws QQConnectException
     * @throws IOException
     */

    @GetMapping("/QQLong")
    public void SetQQLong(HttpServletRequest request, HttpServletResponse response){
        System.err.println("进入：qq跳转");
        /*设置字符集*/
        response.setContentType("text/html;charset=utf-8");
        /*如果 用户信息是空证明用户没有登入 跳转qq登入页面*/
        if(ObjectUtils.isEmpty(ShiroUtils.getSysUser())) {
            try {
                response.sendRedirect(new Oauth().getAuthorizeURL(request));
            } catch (IOException | QQConnectException e) {
                throw new BusinessException("出现错误");
            }
        }
    }


    /**
     *qq登入
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/QQLong")
    @ResponseBody
    public AjaxResult QQLong(HttpServletRequest request, HttpServletResponse response)
    {
        System.err.println("qq登入.........");
        /*loginService.QQLong(request, response)*/
        return AjaxResult.success(loginService.QQLong(request, response));
    }


    @GetMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response)
    {
        /*如果已经登入 返回首页*/
        if (!ObjectUtils.isEmpty(ShiroUtils.getSysUser())){
            return prefix + "/index";
        }
        // 如果是Ajax请求，返回Json字符串。
        if (ServletUtils.isAjaxRequest(request))
        {
            return ServletUtils.renderString(response, "{\"code\":\"1\",\"msg\":\"未登录或登录超时。请重新登录\"}");
        }

        return "login";
    }






    @PostMapping("/login")
    @ResponseBody
    public AjaxResult ajaxLogin(String username, String password, Boolean rememberMe)
    {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
        Subject subject = SecurityUtils.getSubject();
        try
        {
            subject.login(token);
            return success();
        }
        catch (AuthenticationException e)
        {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage()))
            {
                msg = e.getMessage();
            }
            return error(msg);
        }
    }

    @GetMapping("/unauth")
    public String unauth()
    {
        return "error/unauth";
    }


    public Subject getSubject(){
        return SecurityUtils.getSubject();
    }
}
